<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="来源渠道" prop="source">
        <el-select
          v-model="formData.source"
          placeholder="请输入来源渠道"
          clearable
        >
          <el-option label="电话" value="1" />
          <el-option label="邮件" value="2" />
          <el-option label="系统上报" value="3" />
          <el-option label="APP" value="4" />
          <el-option label="微信" value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="机器人类型" prop="robotType">
        <el-select
          v-model="formData.robotType"
          placeholder="请选择机器人类型"
          clearable
        >
          <el-option label="智能盘点机器人" value="1" />
          <el-option label="智能还书系统" value="2" />
          <el-option label="智能导览机器人" value="3" />
        </el-select>
      </el-form-item>

      <el-form-item label="机器人编号" prop="robotCode">
        <el-input v-model="formData.robotCode" placeholder="请输入机器人编号" />
      </el-form-item>
      <el-form-item label="机器人名称" prop="robotName">
        <el-input v-model="formData.robotName" placeholder="请输入机器人名称" />
      </el-form-item>
      <el-form-item label="当前状态" prop="status">
        <el-select
          v-model="formData.status"
          placeholder="请选择当前状态"
          clearable
        >
          <el-option label="待分配" value="1" />
          <el-option label="已分配" value="2" />
          <el-option label="处理中" value="3" />
          <el-option label="待确认" value="4" />
          <el-option label="已完成" value="5" />
          <el-option label="已关闭" value="6" />
          <el-option label="已取消" value="7" />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-select
          v-model="formData.priority"
          placeholder="请选择优先级"
          clearable
        >
          <el-option label="P0" value="P0" />
          <el-option label="P1" value="P1" />
          <el-option label="P2" value="P2" />
          <el-option label="P3" value="P3" />
          <el-option label="P4" value="P4" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品型号" prop="productModel">
        <el-input
          v-model="formData.productModel"
          placeholder="请输入产品型号"
        />
      </el-form-item>
      <el-form-item label="故障代码" prop="errorCode">
        <el-input v-model="formData.errorCode" placeholder="请输入故障代码" />
      </el-form-item>
      <el-form-item label="故障级别" prop="severity">
        <el-select
          v-model="formData.severity"
          placeholder="请选择故障级别"
          clearable
        >
          <el-option label="致命" value="1" />
          <el-option label="严重" value="2" />
          <el-option label="一般" value="3" />
          <el-option label="轻微" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="故障发生时间" prop="failureTime">
        <el-date-picker
          v-model="formData.failureTime"
          type="date"
          value-format="x"
          placeholder="选择故障发生时间"
        />
      </el-form-item>
      <el-form-item label="主要工程师" prop="primaryEngineer">
        <el-input
          v-model="formData.primaryEngineer"
          placeholder="请输入主要工程师"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >确 定</el-button
      >
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {
  RobotOperationApi,
  RobotOperationVO,
} from "@/api/health/robotoperation";

/** 运维工单 表单 */
defineOptions({ name: "RobotOperationForm" });

const { t } = useI18n(); // 国际化
const message = useMessage(); // 消息弹窗

const dialogVisible = ref(false); // 弹窗的是否展示
const dialogTitle = ref(""); // 弹窗的标题
const formLoading = ref(false); // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref(""); // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  libraryId: undefined,
  source: undefined,
  robotType: undefined,
  robotId: undefined,
  robotCode: undefined,
  robotName: undefined,
  status: undefined,
  priority: undefined,
  productModel: undefined,
  errorCode: undefined,
  severity: undefined,
  failureTime: undefined,
  primaryEngineer: undefined,
});
const formRules = reactive({
  robotCode: [
    { required: true, message: "机器人编号不能为空", trigger: "blur" },
  ],
  robotType: [
    { required: true, message: "请选择机器人类型", trigger: "chang" },
  ],
  source: [{ required: true, message: "来源渠道不能为空", trigger: "blur" }],
});
const formRef = ref(); // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true;
  dialogTitle.value = t("action." + type);
  formType.value = type;
  resetForm();
  // 修改时，设置数据
  if (id) {
    formLoading.value = true;
    try {
      formData.value = await RobotOperationApi.getRobotOperation(id);
    } finally {
      formLoading.value = false;
    }
  }
};
defineExpose({ open }); // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(["success"]); // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate();
  // 提交请求
  formLoading.value = true;
  try {
    const data = formData.value as unknown as RobotOperationVO;
    if (formType.value === "create") {
      await RobotOperationApi.createRobotOperation(data);
      message.success(t("common.createSuccess"));
    } else {
      await RobotOperationApi.updateRobotOperation(data);
      message.success(t("common.updateSuccess"));
    }
    dialogVisible.value = false;
    // 发送操作成功的事件
    emit("success");
  } finally {
    formLoading.value = false;
  }
};

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    libraryId: undefined,
    source: undefined,
    robotType: undefined,
    robotId: undefined,
    robotCode: undefined,
    robotName: undefined,
    status: undefined,
    priority: undefined,
    productModel: undefined,
    errorCode: undefined,
    severity: undefined,
    failureTime: undefined,
    primaryEngineer: undefined,
  };
  formRef.value?.resetFields();
};
</script>
